/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Jun 28, 2007
 * 
 */

package org.biojava.services.das.servlets;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import org.biojava.services.das.dao.ProjectBean;
import org.biojava.services.das.registry.UserManager;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProjectIconServlet extends HttpServlet {
	public static final String mimeType = "image/jpg";
	/**
	 * 
	 */
	private static final long serialVersionUID = 297985714957778806L;
	UserManager manager;

	public static final int NOT_FOUND = 404;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		manager=(UserManager)ctx.getBean("userManager");
	}
	
	
	public void doGet(
			HttpServletRequest req, 
			HttpServletResponse res) 
	throws ServletException, IOException {
System.out.println("do get called in ProjectIconServlet");
		String project_idS = req.getParameter("id");

		if (project_idS == null){
			//throw new ServletException("did not find project id");
			res.setStatus(NOT_FOUND);
			return;
		}
		int project_id = -1;
		try {
			project_id = Integer.parseInt(project_idS);
		} catch (NumberFormatException e){
			res.setStatus(NOT_FOUND);
			return;
			//throw new ServletException("could not read project id from provided parameter");
		}

		ProjectBean p = manager.getProjectById(project_id);
		if ( p == null){
			System.err.println("could not find project " + project_id);
			res.setStatus(NOT_FOUND);
			return;
		}

		ImageIcon photo = manager.getProjectImage(p);

		if ( photo == null){
			// a problem occured...
			res.setStatus(NOT_FOUND);
			return;
			//throw new ServletException("did not find a image for the specified project");
		}

	
		BufferedImage bufferedImage = ImageUtil.toBufferedImage(photo.getImage());
		
		byte[] bytes = ImageUtil.toByteArray(bufferedImage,FileUploadServlet.DEFAULT_IMAGE_FORMAT);
		
		
		
		res.setContentType(mimeType);
		res.setContentLength(bytes.length);
		
	
		ByteArrayInputStream in = new ByteArrayInputStream(bytes);
     
        OutputStream out = res.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.close();
		 

	}

	public void service(
			HttpServletRequest arg0, 
			HttpServletResponse arg1) 
	throws ServletException, IOException {	
		super.service(arg0, arg1);
	}





}
